package domain.entities;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

public class Permission implements Serializable {
	private static final long serialVersionUID = -2151570806016270323L;
	
	private int id;
	private String name;

	public Permission() {
	}

	public Permission(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		
		boolean result = true;

		if (!(obj instanceof Permission))
			return false;

		Permission p = (Permission) obj;
		result &= p.getId() == id;
		result &= StringUtils.equals(name, p.getName());

		return result;
	}

	@Override
	public int hashCode() {
		if (fHashCode == 0) {
			fHashCode = 11 + 13 * id;

			if (!StringUtils.isBlank(name)) {
				fHashCode ^= 17 + 23 * name.hashCode();
			}
		}

		return fHashCode;
	}

	@Override
	public String toString() {
		return String.format("%s-%s", id, name);
	}

	private int fHashCode = 0;
}
